package com.linkturing.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.linkturing.serial.AddWeightBean;
import com.linkturing.swing.LtProductQualityBasic;

import redis.clients.jedis.Jedis;

public class JedisUtil {
	
	public static String set(String key,String value) {
		Jedis sjedis = JedisHelper.getJedis();
		return sjedis.set(key, value);
	}
	
	public static String set(String key,String value,int expireTime) {
		Jedis sjedis = JedisHelper.getJedis();
		return sjedis.setex(key, expireTime,value);
	}
	
	public static String get(String key) {
		Jedis sjedis = JedisHelper.getJedis();
		return sjedis.get(key);
	}

	/**

	* 获取给定key中元素个数

	* @param key String

	* @return 元素个数

	* */

	public static long scard(String key) {

		Jedis sjedis = JedisHelper.getJedis();
	
		long len = sjedis.scard(key);
	
	
		return len;

	}
	
	/**

	* 批量获取记录,如果指定的key不存在返回List的对应位置将是null

	* @param keys String

	* @return List<String> 值得集合

	* */

	public List<String> mget(String... keys) {
	Jedis jedis = JedisHelper.getJedis();

	List<String> str = jedis.mget(keys);


	return str;

	}
	
	/**

	* 获取指定范围的记录，可以做为分页使用

	* @param key String

	* @param start long

	* @param end long

	* @return List

	* */
	public static List<String> lrange(String key, long start, long end) {

		Jedis sjedis = JedisHelper.getJedis();
	
		List<String> list = sjedis.lrange(key, start, end);
	
		return list;
	}
	
	public static long lpush(String key ,String value) {
		Jedis sjedis = JedisHelper.getJedis();
		long count = sjedis.lpush(key, value);
		
		return count;
	}
	
	public static long rpush(String key ,String value) {
		Jedis sjedis = JedisHelper.getJedis();
		long count = sjedis.rpush(key, value);
		
		return count;
	}
	
	//存储
	public static void setJsonString(String key, Object obj) {
	    Jedis jedis = JedisHelper.getJedis();
	    jedis.set(key, JSON.toJSONString(obj));
	    jedis.close();
	}
	//获取
	public static String getJsonString(String key) {
	    Jedis jedis = JedisHelper.getJedis();
	    String value = jedis.get(key);
	    jedis.close();
	    return value;
	}
	
	public static List<String> getList(String key){
		Jedis jedis = JedisHelper.getJedis();
		return lrange(key,0,jedis.llen(key));
	}
	
	
	public static String lindex(String key,int index) {
		Jedis jedis = JedisHelper.getJedis();
		return jedis.lindex(key,index); 
	}
	
	public static long detelAll(String key) {
		Jedis jedis = JedisHelper.getJedis();
		return jedis.del(key);
	}
	
	
	public static void resetList(String key,List<LtProductQualityBasic> object) {
		detelAll(key);
		for(int i=0;i<object.size();i++) {
			lpush(key,JSON.toJSONString(object.get(i)));
		}
	}
	
	public static void del(String key,int index) {
		Jedis jedis = JedisHelper.getJedis();
		List<String> list = getList(key);
		if(list!=null) {
			detelAll(key);
			for(int i=0;i<list.size();i++) {
				if(index==i) {
					continue;
				}
				lpush(key,list.get(i));
			}
		}
	}
	
	public static void del(String key,String fenjuan) {
		Jedis jedis = JedisHelper.getJedis();
		List<String> list = getList(key);
		if(list!=null) {
			detelAll(key);
			for(int i=0;i<list.size();i++) {
				LtProductQualityBasic pqb = JSON.toJavaObject(JSON.parseObject(list.get(i)), LtProductQualityBasic.class);
				if(fenjuan.equals(pqb.getVolumeNumber())) {
					continue;
				}
				lpush(key,list.get(i));
			}
		}
	}
	
	public static Set<String> keys(String pattern) {
		Jedis jedis = JedisHelper.getJedis();
		return jedis.keys(pattern);
	}
	
	public static void edit(String key,int index,LtProductQualityBasic pqb) {
		Jedis jedis = JedisHelper.getJedis();
		List<String> list = getList(key);
		if(list!=null) {
			detelAll(key);
			for(int i=0;i<list.size();i++) {
				if(index==i) {
					rpush(key,JSON.toJSONString(pqb));
					continue;
				}
				rpush(key,list.get(i));
			}
		}else{
			rpush(key,JSON.toJSONString(pqb));
		}
		
		
	}
	
	public static void editAddWeight(String key,int index,AddWeightBean pqb) {
		Jedis jedis = JedisHelper.getJedis();
		List<String> list = getList(key);
		if(list!=null) {
			detelAll(key);
			for(int i=0;i<list.size();i++) {
				if(index==i) {
					rpush(key,JSON.toJSONString(pqb));
					continue;
				}
				rpush(key,list.get(i));
			}
		}
		
		
	}
	
	public static void main(String[] args) {
		LtProductQualityBasic pqb = new LtProductQualityBasic();
		pqb.setId("2");
		pqb.setLength(121);
		
		List<LtProductQualityBasic> pqbList = new ArrayList<LtProductQualityBasic>();
		pqbList.add(pqb);
		
//		setJsonString("json-2021-3-16",pqbList);
		
		lpush("111",JSON.toJSONString(pqb));
		List<String> mList = lrange("7777-1",0,100);
		
		System.out.println(JSON.toJSONString(mList));
	}
	
}
